<div id="operations">
<?php 
//print_r($currentObject);
?>

<?php 




//echo $currentObject->options_value;
$options=str_split($currentObject->options_value);

$operation=array("List","Add","Edit","View","ReadOnly","HiddenAdd","ChildLookup");
$ops=array("L","A","C","V","R","K","O");

$i=0;
foreach ($operation as $op)
{
	$checked="";
	if (in_array($ops[$i], $options)) $checked="checked";
?>

<input type="checkbox" name="operation" <?=$checked?> id="operations_<?=$op?>" value="<?=$ops[$i]?>" /> <?=$op?> &nbsp; 

<script>
$('#operations_<?=$op?>').change(function () {
	str=$("#ButterflyPortal_data_OPTIONS_VALUE").val();
	value=$(this).attr("value");
	str=str.replace(value,"");
if($(this).is(":checked"))
{
	
	str=str+value;
	

}else{

	
}

$("#ButterflyPortal_data_OPTIONS_VALUE").val(str);
});
</script>
<?php
$i++;
}
?>
</div>
<script>

$("#ButterflyPortal_data_OPTIONS_VALUE").hide();

//$("#ButterflyPortal_data_OPTIONS").attr("type","hidden");
$("#el_OPTIONS_VALUE").append($("#operations"));


</script>

<div id="VALUES_DESCRIPTION_div" >
Add Field  <img src="./images/icons/ButterflyPortal-add.png" title="Add" id="add" width="20" style="vertical-align: sub;">
<div id="VALUES_DESCRIPTION_div_0" style="display: inline"><select id="VALUES_DESCRIPTION_0" name="VALUES_DESCRIPTION"></select><img name="minus" src="./images/icons/ButterflyPortal-delete.png" title="Minus" id="minus" index="0" width="20" style="vertical-align: sub;"></div>
<div id="new_VALUES_DESCRIPTION" style="display: inline" ></div>

</div>


<div id="SUB_VALUES_DESCRIPTION_div" >
Add Field  <img src="./images/icons/ButterflyPortal-add.png" title="Add" id="sub_add" width="20" style="vertical-align: sub;">
<div id="SUB_VALUES_DESCRIPTION_div_0" style="display: inline"><select id="SUB_VALUES_DESCRIPTION_0" name="SUB_VALUES_DESCRIPTION"></select><img name="sub_minus" src="./images/icons/ButterflyPortal-delete.png" title="Minus" id="sub_minus" index="0" width="20" style="vertical-align: sub;"></div>
<div id="new_SUB_VALUES_DESCRIPTION" style="display: inline" ></div>

</div>

<script>
i = 0;
$("#el_VALUES_DESCRIPTION").append($("#VALUES_DESCRIPTION_div"));
$("#add").click(function() {
 // $( "#new_VALUES_DESCRIPTION" ).append('<input class="ButterflyPortal-input-1" type="text"  id="ButterflyPortal_data_VALUES_DESCRIPTION_'+i+'" value="" size="60" maxlength="255"/><br/>');
 
 $("#VALUES_DESCRIPTION_div_0").clone(true).appendTo( "#new_VALUES_DESCRIPTION" ).removeAttr('id').attr("id","VALUES_DESCRIPTION_div_"+i);
 $("#VALUES_DESCRIPTION_div_"+i+" :first-child").removeAttr('id').attr("id","VALUES_DESCRIPTION_"+i);
 $("#VALUES_DESCRIPTION_div_"+i+" img").attr("index",i);
	str= $("#ButterflyPortal_data_VALUES_DESCRIPTION").val();
 $("#ButterflyPortal_data_VALUES_DESCRIPTION").val(str+","+$("#VALUES_DESCRIPTION_0 option:first").val());
  i++;

});




j = 0;
$("#el_SUB_VALUES_DESCRIPTION").append($("#SUB_VALUES_DESCRIPTION_div"));
$("#sub_add").click(function() {
 // $( "#new_VALUES_DESCRIPTION" ).append('<input class="ButterflyPortal-input-1" type="text"  id="ButterflyPortal_data_VALUES_DESCRIPTION_'+i+'" value="" size="60" maxlength="255"/><br/>');
 
 $("#SUB_VALUES_DESCRIPTION_div_0").clone(true).appendTo( "#new_SUB_VALUES_DESCRIPTION" ).removeAttr('id').attr("id","SUB_VALUES_DESCRIPTION_div_"+j);
 $("#SUB_VALUES_DESCRIPTION_div_"+j+" :first-child").removeAttr('id').attr("id","SUB_VALUES_DESCRIPTION_"+j);
 $("#SUB_VALUES_DESCRIPTION_div_"+j+" img").attr("index",j);
	str= $("#ButterflyPortal_data_SUB_VALUES_DESCRIPTION").val();
 $("#ButterflyPortal_data_SUB_VALUES_DESCRIPTION").val(str+","+$("#SUB_VALUES_DESCRIPTION_0 option:first").val());
  j++;

});





function prepare_VALUES_DESCRIPTION()
{
<?php
$VALUES_DESCRIPTION=preg_replace("/,/", "','",  $currentObject->values_description);

?>
current_VALUES_DESCRIPTION=new Array('<?=$VALUES_DESCRIPTION?>');
var length = current_VALUES_DESCRIPTION.length;

for (; i < length; i++) {
element = current_VALUES_DESCRIPTION[i];
if (i>0) {
	$("#VALUES_DESCRIPTION_div_0").clone(true).appendTo( "#new_VALUES_DESCRIPTION" ).removeAttr('id').attr("id","VALUES_DESCRIPTION_div_"+i);

	$("#VALUES_DESCRIPTION_div_"+i+" :first-child").removeAttr('id').attr("id","VALUES_DESCRIPTION_"+i);
		$("#VALUES_DESCRIPTION_div_"+i+" img").attr("index",i);
	
}
$("#VALUES_DESCRIPTION_"+i).val(element);
}
}




function prepare_SUB_VALUES_DESCRIPTION()
{
<?php
$SUB_VALUES_DESCRIPTION=preg_replace("/,/", "','",  $currentObject->sub_values_description);

?>
current_VALUES_DESCRIPTION=new Array('<?=$SUB_VALUES_DESCRIPTION?>');
var length = current_VALUES_DESCRIPTION.length;

for (; j < length; j++) {
element = current_VALUES_DESCRIPTION[i];
if (j>0) {
	$("#VALUES_DESCRIPTION_div_0").clone(true).appendTo( "#new_SUB_VALUES_DESCRIPTION" ).removeAttr('id').attr("id","SUB_VALUES_DESCRIPTION_div_"+j);

	$("#SUB_VALUES_DESCRIPTION_div_"+j+" :first-child").removeAttr('id').attr("id","SUB_VALUES_DESCRIPTION_"+j);
		$("#SUB_VALUES_DESCRIPTION_div_"+j+" img").attr("index",j);
	
}
$("#SUB_VALUES_DESCRIPTION_"+j).val(element);
}
}

function getColumns(table,column,description,selectedcolumn,selecteddescription)
{


	$.get("/portlet.php",
			{app: "system",
			operation:"getTableComuns",
			table:table},
			   function(data){

		
			

				 $("#el_"+column).parent().parent().show();
	               <?php unBlockUI(); ?>
			if (data!="") {
				//alert(data);
				//	location.reload(true);
				selectedval=$('#ButterflyPortal_data_'+description).val();
				if (selectedval==null) selectedval=selecteddescription;
				
				//alert(selectedval);
			selectValues=data.split(",");
			$('#'+description+'_0').empty();
				$.each(selectValues, function(key) {  
				//	alert(key); 
				selected="notselected";
				if (selectValues[key]==selectedval) selected="selected";
				     $('#'+description+'_0').append($("<option></option>").attr(selected, selected).attr("value",selectValues[key]).text(selectValues[key])); 
				});

				if (selectedval=="") $('#ButterflyPortal_data_'+description).val($("#"+description+"_0 option:first").val());

				selectedval=$('#ButterflyPortal_data_'+column).val();
				if (selectedval==null) selectedval=selectedcolumn;
				selectValues=data.split(",");
				$('#ButterflyPortal_data_'+column).empty();
				$.each(selectValues, function(key) {  
				//	alert(key); 
				selected="notselected";
				if (selectValues[key]==selectedval) selected="selected";
				     $('#ButterflyPortal_data_'+column) .append($("<option></option>").attr(selected, selected) .attr("value",selectValues[key])  .text(selectValues[key])); 
				});

				if (column.indexOf("SUB_")!=0) prepare_VALUES_DESCRIPTION();
				else
				prepare_SUB_VALUES_DESCRIPTION();
				
				
				}
			//else  alert("There was an error, please contact your administrator !");
			

			   });

	
	<?php blockUI($var_portal_loading); ?>	
	}

$("#ButterflyPortal_data_VALUES_TABLE").change(function() {

//	alert($(this).val());

	//getTableComuns

getColumns($(this).val(),"VALUES_COLUMN","VALUES_DESCRIPTION","<?=$currentObject->values_column?>","<?=$currentObject->values_description?>");
	//alert("ok");
	
});


$("#ButterflyPortal_data_SUB_VALUES_TABLE").change(function() {

//	alert($(this).val());

	//getTableComuns

getColumns($(this).val(),"SUB_VALUES_COLUMN","SUB_VALUES_DESCRIPTION","<?=$currentObject->sub_values_column?>","<?=$currentObject->sub_values_description?>");
	//alert("ok");
	
});

getColumns($("#ButterflyPortal_data_VALUES_TABLE").val(),"VALUES_COLUMN","VALUES_DESCRIPTION");

getColumns($("#ButterflyPortal_data_SUB_VALUES_TABLE").val(),"SUB_VALUES_COLUMN","SUB_VALUES_DESCRIPTION");

$(function() {
	
$('select[name="VALUES_DESCRIPTION"]').change(function () {
	str="";
	for (j=0;j<i;j++){
	
	
	if ($("#VALUES_DESCRIPTION_"+j).val()!="undefined"){
	str+=$("#VALUES_DESCRIPTION_"+j).val();
	if (j<i-1) str+=",";}
	}
	
	$("#ButterflyPortal_data_VALUES_DESCRIPTION").val(str);


});


$('select[name="SUB_VALUES_DESCRIPTION"]').change(function () {
	str="";
	for (j=0;j<i;j++){
	
	
	if ($("#SUB_VALUES_DESCRIPTION_"+j).val()!="undefined"){
	str+=$("#SUB_VALUES_DESCRIPTION_"+j).val();
	if (j<i-1) str+=",";}
	}
	
	$("#ButterflyPortal_data_SUB_VALUES_DESCRIPTION").val(str);


});



});


function cleanArray(actual){
	  var newArray = new Array();
	  for(var i = 0; i<actual.length; i++){
	      if (actual[i]){
	        newArray.push(actual[i]);
	    }
	  }
	  return newArray;
	}

	
$("img[name='minus']").click(function() {
	str=$("#ButterflyPortal_data_VALUES_DESCRIPTION").val();
	index=$(this).attr("index");
	value=$("#VALUES_DESCRIPTION_"+index).val();
	str=str.replace(value,"");
	str=cleanArray(str.split(',')).join(",");

	$("#ButterflyPortal_data_VALUES_DESCRIPTION").val(str);
	$("#VALUES_DESCRIPTION_div_"+index).remove();
	
	});


$("img[name='sub_minus']").click(function() {
	str=$("#ButterflyPortal_data_SUB_VALUES_DESCRIPTION").val();
	index=$(this).attr("index");
	value=$("#SUB_VALUES_DESCRIPTION_"+index).val();
	str=str.replace(value,"");
	str=cleanArray(str.split(',')).join(",");

	$("#ButterflyPortal_data_SUB_VALUES_DESCRIPTION").val(str);
	$("#SUB_VALUES_DESCRIPTION_div_"+index).remove();
	
	});

//$("#ButterflyPortal_data_VALUES_DESCRIPTION").hide();

var Select=new Array("VALUES_FILTERS","VALUES_DESCRIPTION","VALUES_COLUMN","VALUES_TABLE","SUB_VALUES_FILTERS","SUB_VALUES_DESCRIPTION","SUB_VALUES_COLUMN","SUB_VALUES_TABLE");
var TextField=new Array();
var Hidden=new Array();
var Radio=new Array("VALUES_FILTERS","VALUES_DESCRIPTION","VALUES_COLUMN","VALUES_TABLE");
var Checkbox=new Array("VALUES_FILTERS","VALUES_DESCRIPTION","VALUES_COLUMN","VALUES_TABLE");
var TextArea=new Array("TEXTAREA_ROWS","TEXTAREA_COLS");
var Upload=new Array("UPLOADPATH");
var Password=new Array();
var RichText=new Array("TEXTAREA_ROWS","TEXTAREA_COLS");
var PortalImage=new Array("UPLOADPATH");

function hideType(arr){
	for (var i=0;i<arr.length;i++){

	$("#el_"+arr[i]).parent().parent().hide();
		
	}}

function showType(arr){

	<?php 
			$operation=get_enum_values("portal_tables_fields", "edittag");

		
			foreach ($operation as $op)
			{
				
			?>
				if (arr=="<?=$op?>") arr=<?=$op?>;
			<?php }?>
				
	for (var i=0;i<arr.length;i++){

	$("#el_"+arr[i]).parent().parent().show();
		
	}}



$("input[name='radio_ButterflyPortal_data_EDITTAG']").change(function () {

	<?php 



foreach ($operation as $op)
{
	
?>
	hideType(<?=$op?>);
<?php }?>


	showType($(this).val());

});

<?php 


	
		foreach ($operation as $op)
		{
			
		?>
			hideType(<?=$op?>);
		<?php }?>


			showType("<?=$currentObject->edittag?>");

</script>

